class rb_title extends eui.Image{
	public constructor(par:gpage2) {
		super();
		this.init_event();
		this.parent_page = par;
	}

	private parent_page:gpage2;

	public init_event(){
		this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touch_begin_hander, this);
		this.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.touch_end_hander, this);
		this.addEventListener(egret.TouchEvent.	TOUCH_END, this.touch_end_hander, this);		
	}

	//1.正常 2.拖拽 3.到位  4.待消亡
	public states:number;
	public movePos:egret.Point;
	public speed:number = 1;
	public distance:number = 0;
	public stage_width:number = 0;
	public start_time:number = 0;
	public cur_id:number = 0;
	public img_arr:Object;
	public end_x:number = 0;
	public is_stag:boolean = false;
	public big_type:number = 0;
	public cur_move:number = 0;
	public next_time:number = 0;
	public set_info(defalut_pos:egret.Point,velocity:number){

		this.set_state(1);
		this.next_time = this.start_time = egret.getTimer();
		this.alpha = 1;

		var r_arr:Array<number> = utils.CoreData.instance.get_rand_img();
		var res_str:string = r_arr[0]+'_'+r_arr[1]+'_png';
		this.big_type = r_arr[0];

		console.log('res_str='+res_str);
		this.source = RES.getRes(res_str);

		var oldw = this.width;
		var oldh = this.height;

		this.width = utils.CoreData.instance.rb_width;
		this.height = oldh * (utils.CoreData.instance.rb_width/oldw);
		this.anchorOffsetY = this.height*0.5;
		this.anchorOffsetX = this.width*0.5;
		this.movePos = defalut_pos;
		this.end_x = -this.width*0.5;
	
		this.speed = velocity;

		this.stage_width = 640;
		this.distance = defalut_pos.x;

		// console.log('defalut_pos='+defalut_pos.x);
		// this.movePos.x = this.stage_width + this.width;
		this.x = this.movePos.x;
		this.y = this.movePos.y;

		console.log("rpos="+this.x+'--'+this.y);

		
		
	}

	public process(){
		// s = v*t
		if(this.states<=2){
			var ctime:number = (egret.getTimer() - this.start_time)*0.001;
			if(ctime<=0){
				ctime = 0.001;
			}
			// var curx:number = this.distance - (this.parent_page.rb_speed * ctime);

			var c_time:number = (egret.getTimer() - this.next_time)*0.001;
			if(c_time<=0){
				c_time = 0.001;
			}
			this.cur_move += c_time * this.parent_page.rb_speed;
			this.next_time = egret.getTimer();

			var curx:number = this.distance - this.cur_move;

			this.movePos.x = curx;

			if(this.states == 1){
				this.x = curx;
				this.y = this.movePos.y;
				if(curx<=this.end_x){
					this.clear();
				}

				// console.log('x='+curx+'y='+this.y);
			}
			else if(this.states == 2){
				var pos:egret.Point = this.parent.globalToLocal(utils.CoreData.instance.StageX,utils.CoreData.instance.StageY);
				// console.log(pos);
				this.x = pos.x;
				this.y = pos.y;
				
				var tag_img:eui.Image = this.parent_page.tong_list[this.big_type-1];
				var target_pos:egret.Point = new egret.Point(tag_img.x,tag_img.y);

				var dis=Math.sqrt((target_pos.x-this.x)*(target_pos.x-this.x)+(target_pos.y-this.y)*(target_pos.y-this.y));
				if(dis<=60)
				{
					this.set_state(3);
					egret.Tween.get(tag_img).to({'scaleX':1.2,'scaleY':1.2},200)
					.to({'scaleX':1,'scaleY':1},100);
				}
			}
		}
	}

	public set_state(c_state){
		this.states = c_state;
		if(this.states == 1){
			
		}
		else if(this.states == 3){
			// this.alpha
			// gret.Tween.get(this.img_item4).to({"y":238},500).wait(100).to({"y":218},300);
			egret.Tween.get(this).to({'alpha':0},300).call(function(){
				this.clear();
				console.log('im clear');
			},this);
			this.parent_page.add_source();
		}
	}

	public clear(){
		if(this.parent)
			this.parent.removeChild(this);
		
		
		this.set_state(4);
	}

	public touch_begin_hander(e:egret.TouchEvent){
		this.parent_page.stop_other();
		this.set_state(2);
		this.is_stag = true;
		utils.CoreData.instance.StageX = e.stageX;
		utils.CoreData.instance.StageY = e.stageY;
	}

	public touch_end_hander(e:egret.TouchEvent){
		this.touch_end();
	}

	public touch_end(){
		if(this.states==2){
			this.is_stag = false;
			this.set_state(1);
		}
	}
}